layui.define(['admin', 'form'], function (exports) {
    var $ = layui.$, admin = layui.admin, form = layui.form;

    admin.events.addTr = function () {
        var t = this;
        var body = $(t).parents('table').find("tbody");
        var type = $(t).data('type');
        var trnum = $(body).find("tr").length;
        if (type == 1) {
            var tr = '<tr>\n' +
                '                <td><input type="text" class="layui-input" name="sale_rate[' + trnum + '][performance]">\n' +
                '                </td>\n' +
                '                <td><input type="text" class="layui-input" name="sale_rate[' + trnum + '][rate]"></td>\n' +
                '                <td>\n' +
                '                    <button class="layui-btn layui-btn-sm" layadmin-event="delTr">删除</button>\n' +
                '                </td>\n' +
                '            </tr>';
        }
        if (type == 2) {
            var tr = '<tr>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate1[' + trnum + '][recount]"></td>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate1[' + trnum + '][nums]"></td>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate1[' + trnum + '][rate]"></td>\n' +
                '                                        <td>\n' +
                '                                            <button class="layui-btn layui-btn-sm" layadmin-event="delTr">删除</button>\n' +
                '                                        </td>\n' +
                '                                    </tr>';
        }
        if (type == 3) {
            var tr = '<tr>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate2[' + trnum + '][recount]"></td>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate2[' + trnum + '][nums]"></td>\n' +
                '                                        <td><input type="text" class="layui-input" name="rebuy_rate2[' + trnum + '][rate]"></td>\n' +
                '                                        <td>\n' +
                '                                            <button class="layui-btn layui-btn-sm" layadmin-event="delTr">删除</button>\n' +
                '                                        </td>\n' +
                '                                    </tr>';
        }
        if (type == 4) {
            var tr = '<tr>\n' +
                '                                        <td><input type="text" class="layui-input" name="shop_rate[' + trnum + '][nums]"></td>\n' +
                '                                        <td><input type="text" class="layui-input" name="shop_rate[' + trnum + '][rate]"></td>\n' +
                '                                        <td>\n' +
                '                                            <button class="layui-btn layui-btn-sm" layadmin-event="delTr">删除</button>\n' +
                '                                        </td>\n' +
                '                                    </tr>';
        }
        //移除所有之前等级删除按钮，保证序号正确
        $(body).find(".layui-btn").addClass('layui-hide');
        $(body).append(tr);
    }

    admin.events.delTr = function () {
        var tbody = $(this).parents('tbody');
        $(this).parents('tr').remove();
        //最后面的一个tr显示删除
        $(tbody).find("tr:last").find('.layui-btn').removeClass('layui-hide')

    }


    //提交编辑
    form.on('submit(edit)', function (data) {
        console.log(data.field);
        admin.req({
            url: '/shengteng/Bonus/setting'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    // layer.closeAll("iframe");
                    //刷新父页面
                    // parent.location.reload();
                    location.reload()
                });
            }
        });
    });

    exports('commission_setting', {});
});
